package com.hzqx.xxzx.framework.oss;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.InputStream;
import java.util.UUID;

@Slf4j
public class SimpleOSSClient implements OSSClient {
    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String bucketName;
    private String fileBasePath;

    /**
     * oss原生客户端
     */
    private OSS oss;

    private SimpleOSSClient() {
    }

    public SimpleOSSClient(String endpoint, String accessKey, String secretKey, String bucketName, String fileBasePath) {
        this.endpoint = endpoint;
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucketName = bucketName;
        this.fileBasePath = fileBasePath;
        this.oss = new OSSClientBuilder().build(this.endpoint, this.accessKey, this.secretKey);
    }


    /**
     * 上传文件
     */
    public OSSUploadFileResponse putObject(File file) {
        OSSUploadFileResponse ossUploadFileResponse = this.buildOSSUploadFileResponse(file.getName());
        this.oss.putObject(bucketName, ossUploadFileResponse.getFileName(), file);
        log.info(JSON.toJSONString(ossUploadFileResponse));
        return ossUploadFileResponse;
    }

    @Override
    public OSSUploadFileResponse putObject(InputStream file) {
        OSSUploadFileResponse ossUploadFileResponse = this.buildOSSUploadFileResponse("default.jpg");
        this.oss.putObject(bucketName, ossUploadFileResponse.getFileName(), file);
        log.info(JSON.toJSONString(ossUploadFileResponse));
        return ossUploadFileResponse;
    }

    public OSSUploadFileResponse putObject(InputStream file, String fileName) {
        OSSUploadFileResponse ossUploadFileResponse = this.buildOSSUploadFileResponse(fileName);
        this.oss.putObject(bucketName, ossUploadFileResponse.getFileName(), file);
        log.info(JSON.toJSONString(ossUploadFileResponse));
        return ossUploadFileResponse;
    }

    /**
     * 生成一个UUID
     */
    private String buildUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 初始化一个文件上传响应对象
     */
    private OSSUploadFileResponse buildOSSUploadFileResponse(String fileName) {
        String id = this.buildUUID();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        OSSUploadFileResponse ossUploadFileResponse = new OSSUploadFileResponse();
        ossUploadFileResponse.setId(id);
        ossUploadFileResponse.setOldFileName(fileName);
        ossUploadFileResponse.setFileName(id + "." + suffix);
        ossUploadFileResponse.setRelativePath(id + "." + suffix);
        ossUploadFileResponse.setAbsolutePath(this.fileBasePath + "/" + id + "." + suffix);
        return ossUploadFileResponse;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public String getAccessKey() {
        return accessKey;
    }


    public String getSecretKey() {
        return secretKey;
    }


    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public OSS getOss() {
        return oss;
    }


    public static void main(String[] args) {
        OSSClient ossClient = new SimpleOSSClient("http://oss-cn-hangzhou.aliyuncs.com", "LTAI4GDepDXdbRWBexoTkEAi", "finP6I1He4F5UauUD3S0FzHeBOlYL5", "flm-ces", "");
        ossClient.putObject(new File("D:\\红码.jpg"));
    }
}
